package org.blade.personal.framework.entity;

import java.lang.reflect.Field;

import org.blade.personal.framework.components.JavaTypeEnum;
import org.blade.personal.framework.components.annotation.Column;
import org.blade.personal.framework.components.annotation.Table;
import org.blade.personal.framework.orm.EntityAlias;
import org.blade.personal.framework.orm.EntityAttribute;
import org.blade.personal.framework.orm.TypeAlias;
import org.blade.personal.framework.orm.base.FloatAlias;
import org.blade.personal.framework.orm.base.IntegerAlias;
import org.blade.personal.framework.orm.base.StringAlias;
import org.blade.personal.framework.orm.ext.TypeAliesExtendHandler;
import org.blade.personal.framework.orm.ext.concre.SimpleTypeAliesExtendHandler;

/**
 * 抽象实体别名
 * 
 * @author ZGF
 *
 * @param <T>
 * @version 0.1
 */
public abstract class AbstractEntityAlies<T> implements EntityAlias<T> {

	/**
	 * The alies of table.
	 */
	protected String alies;

	private String tableName;

	/**
	 * 构造函数,用来初始化子类的各属性的对象
	 * 
	 * @param alies
	 */
	public AbstractEntityAlies(String alies) {
		this.alies = alies;
		Field[] fields = getClass().getFields();
		Table table = getClass().getAnnotation(Table.class);
		this.tableName = table.name();
		TypeAliesExtendHandler TypeAliesExtendHandler = new SimpleTypeAliesExtendHandler();
		for (Field field : fields) {
			Column column = field.getAnnotation(Column.class);
			try {
				
				if("java.lang.Integer".equals(column.javaType())){
					field.setAccessible(true);
					TypeAlias<Integer> typeAlies = new IntegerAlias(column.name(),
							0, this.tableName, this.alies, column.javaType());
					EntityAttribute<Integer> attribute = (EntityAttribute<Integer>) typeAlies;
					attribute.setOwner(this);
					field.set(this, typeAlies);
					field.setAccessible(false);
					
				}else if("java.lang.String".equals(column.javaType())){
					field.setAccessible(true);
					TypeAlias<String> typeAlies = new StringAlias(column.name(),
							"", this.tableName, this.alies, column.javaType());
					EntityAttribute<Integer> attribute = (EntityAttribute<Integer>) typeAlies;
					attribute.setOwner(this);
					field.set(this, typeAlies);
					field.setAccessible(false);
					
				} else if("java.lang.Float".equals(column.javaType())){
					field.setAccessible(true);
					TypeAlias<Float> typeAlies = new FloatAlias(column.name(),
							0F, this.tableName, this.alies, column.javaType());
					EntityAttribute<Float> attribute = (EntityAttribute<Float>) typeAlies;
					attribute.setOwner((EntityAlias<Integer>)this);
					field.set(this, typeAlies);
					field.setAccessible(false);
					
				}else{
					field.setAccessible(true);
					TypeAlias typeAlies = TypeAliesExtendHandler
							.extend(column.javaType());
					EntityAttribute attribute = (EntityAttribute) typeAlies;
					attribute.setOwner(this);
					field.set(this, typeAlies);
					field.setAccessible(false);
				}
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	/**
	 * 重写默认构造函数
	 */
	public AbstractEntityAlies() {
		this(null);
	}

	@Override
	public String getEntityName() {
		return this.getClass().getName();
	}

	@Override
	public String getTable() {
		return this.tableName;
	}

	@Override
	public String getAlise() {
		return this.alies;
	}

}
